﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//演示 Json文件保存List集合与打开
//演示部分分为 固定名称 固定位置的保存与任意名称与任意位置的保存

//使用Json需先添加引用 Tools->NuGet Package Manager->Package Manager Console
//在PM后输入命令：Install-Package Newtonsoft.Json即可
namespace Json格式文件保存与打开
{
    public partial class Form1 : Form
    {
        List<RectangleGraphicDemo> rectList = new List<RectangleGraphicDemo>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (RectangleGraphicDemo demo in rectList)
            {
                e.Graphics.DrawRectangle(Pens.Green, demo.rect);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RectangleGraphicDemo demo1 = new RectangleGraphicDemo();
            Rectangle rect1 = new Rectangle(new Point(40, 40), new Size(200, 100));
            demo1.rect = rect1;

            RectangleGraphicDemo demo2 = new RectangleGraphicDemo();
            Rectangle rect2 = new Rectangle(new Point(80, 80), new Size(200, 100));
            demo2.rect = rect2;

            rectList.Add(demo1);
            rectList.Add(demo2);
        }
        //任意名称的Json文件保存
        private void ArbitraryName_Click(object sender, EventArgs e)
        {
            //保存
            SaveFileDialog dlg = new SaveFileDialog
            {
                //设置一个打开的初始目录
                InitialDirectory = Directory.GetCurrentDirectory(),
                Filter = "json files (*.json)|*.json",
                FilterIndex = 1,
                RestoreDirectory = true
            };

            //将各个图元数据list数据添加进要序列化的数据list
            List<JObject> JObjList = new List<JObject>();
            AddObjToSerializeDataList<RectangleGraphicDemo>(JObjList, rectList);
            //调用序列化函数对数据list进行序列化
            string jsonStr = JsonConvert.SerializeObject(JObjList, Formatting.Indented, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            });

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string FilePath = dlg.FileName;
                File.WriteAllText(FilePath, jsonStr);
            };
        }

        private void FixedName_Click(object sender, EventArgs e)
        {
            //弹出窗口 直接选择路径
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            string foldPath = "";
            //将各个图元数据list数据添加进要序列化的数据list
            List<JObject> JObjList = new List<JObject>();
            AddObjToSerializeDataList<RectangleGraphicDemo>(JObjList, rectList);
            //调用序列化函数对数据list进行序列化
            string jsonStr = JsonConvert.SerializeObject(JObjList, Formatting.Indented, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            });
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                foldPath = dialog.SelectedPath + @"\" + @"\ConfigHeaterStrip.json";
                File.WriteAllText(foldPath, jsonStr);
                MessageBox.Show(" 生成ConfigHeaterStrip文件成功!", "提示");
            }
            else
            {
                MessageBox.Show(" 请选择文件生成路径!", "提示");
            }
        }

        private void AddObjToSerializeDataList<T>(List<JObject> list, List<T> GraphList)
        {
            foreach (dynamic obj in GraphList)
            {
                JObject JObj = (JObject)JToken.FromObject(obj);
                list.Add(JObj);
            }
        }

        //打开Json文件
        private void Open_Click(object sender, EventArgs e)
        {
            //打开
            OpenFileDialog dlg = new OpenFileDialog
            {
                InitialDirectory = Directory.GetCurrentDirectory(),
                Filter = "json files (*.json)|*.json",
                FilterIndex = 1,
                RestoreDirectory = true
            };
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string FilePath = dlg.FileName;
                string jsonStr = File.ReadAllText(FilePath);
                List<JObject> varList = new List<JObject>();
                //调用反序列化函数得到JObject对象list
                varList = JsonConvert.DeserializeObject<List<JObject>>(jsonStr);
                //遍历JObject对象list
                foreach (JObject varobj in varList)
                {
                    //使用JObject对象的键值获取方法得到数据对象类型
                    string str = varobj["type"].ToString();

                    //根据对象类型进行对象重构
                    if (int.Parse(str) == 1)
                    {
                        RectangleGraphicDemo demo = varobj.ToObject<RectangleGraphicDemo>();
                        rectList.Add(demo);
                        Invalidate();
                    }    
                }
            }
        }

        private void clear_Click(object sender, EventArgs e)
        {
            rectList.Clear();
            Invalidate();
        }

        
    }
}
